package com.z.kq.controller;

import com.github.pagehelper.PageInfo;
import com.z.kq.entity.Sign;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.aop.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.vo.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.z.kq.service.ISignService;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;

/**
 * 打卡记录Controller
 *
 * @author z
 * @date 2023-09-28
 */
@Api(tags = "打卡记录")
@RestController
@RequestMapping("/kq/sign")
public class SignController extends BaseController {

    private String prefix = "kq/sign";

    @Autowired
    private ISignService signService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/kq/sign/main','kq:sign:main')")
    @ApiOperation(value = "查询页面")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询打卡记录列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/kq/sign/data','kq:sign:main')")
    @Log(title = "打卡记录", describe = "查询打卡记录列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute Sign sign, PageVo pageVo) {
        PageInfo<Sign> pageInfo = signService.selectSignPage(sign, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增打卡记录
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/kq/sign/add','kq:sign:add')")
    @ApiOperation(value = "添加页面")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增打卡记录
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/kq/sign/save','kq:sign:add')")
    @Log(title = "打卡记录", describe = "新增打卡记录数据", type = BusinessType.ADD)
    @ApiOperation(value = "添加接口")
    public Result save(@RequestBody Sign sign) {
        return Result.decide(signService.save(sign));
    }

    /**
     * 修改打卡记录
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/kq/sign/edit','kq:sign:edit')")
    @ApiOperation(value = "修改页面")
    public ModelAndView edit(Long id, ModelMap map) {
        Sign sign =signService.getById(id);
        map.put("sign", sign);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改打卡记录
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/kq/sign/update','kq:sign:edit')")
    @Log(title = "打卡记录", describe = "修改打卡记录数据", type = BusinessType.EDIT)
    @ApiOperation(value = "修改接口")
    public Result update(@RequestBody Sign sign) {
        return Result.decide(signService.updateById(sign));
    }

    /**
     * 删除打卡记录
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/kq/sign/batchRemove','kq:sign:del')")
    @Log(title = "打卡记录", describe = "删除打卡记录数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "批量删除接口")
    public Result batchRemove(String ids) {
        return Result.decide(signService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除打卡记录
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/kq/sign/remove','kq:sign:del')")
    @Log(title = "打卡记录", describe = "删除打卡记录数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "删除接口")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(signService.removeById(id));
    }
}
